<?php
/**
 *  
 *
  */
namespace Org\Util;
class Jscqnc {

    
     public function cqnc_balance() {
		$condition['c_id']=8;
		$condition['status']=0;
 		$order=M('lottery_order')->where($condition)->select();  //所有没有结算订单
		
		$period=M('lottery_order')->distinct(true)->field('c_no')->where($condition)->select();  //没结算期数
		foreach($period as $k=>$v){
 		  $condition1['c_id']=8;
		  $condition1['c_no']=$v['c_no'];
		  $cqssc_result[$v['c_no']]=M('lottery_results')->where($condition1)->getField('lottery_result');  //根据期数获取开奖结果
 		}
		 
		foreach($order as $ok=>$ov){
			
 		   if($cqssc_result[$ov['c_no']]){    //有开奖结果才结算
		     
 		      $this->jiesuan($cqssc_result[$ov['c_no']],$ov);
			
			  }
 		}
		return; 		
     }
	
	 public function jiesuan($result,$info=array())
	 {
		 
		 $num= explode(",",$result);
		 $sum=array_sum($num);
 		 
		 //连码
		
		  if(substr($info['w_type'],0,2)=='LM'){
 			 $this->lm($num,$info);
			 
			 }
		
		 //总和大小玩法 ZDX
		 if($info['w_type']=='ZDX'){
 			 $this->zh_dx($sum,$info);
			 }
			 //总和尾大小玩法 ZWDX
		 if($info['w_type']=='ZWDX'){
 			 $this->zh_wdx($sum,$info);
			 }
		 //正码玩法 ZM
		 if($info['w_type']=='ZM'){
			 
 			 $this->zm($num,$info);
			 }
		 //总和单双玩法 ZDS
		 if($info['w_type']=='ZDS'){
			 $this->zh_ds($sum,$info);
			 }
		
		 //龙虎和玩法 LH L H T
		 $lh55=substr($info['w_type'],0,2);
		  if($lh55=='LH'){
		   $this->lht($num,$info);
		   }
		 //1-8球玩法  B1-5  0-9球  大小单双
		 for($i=1;$i<=8;$i++){
 			  if($info['w_type']=='DX'.$i){ //1-8大小
			 
			   $this->bdx($num,$info);
			   }
			   
			   if($info['w_type']=='DS'.$i){ //1-8单双
			   $this->bds($num,$info);
			   } 
			    if($info['w_type']=='WDX'.$i){ //1-8尾大小
			   $this->bdx_w($num,$info);
			   } 
			   if($info['w_type']=='B'.$i){ //1-8 号码
			   $this->ball($num,$info);
			   }
			   if($info['w_type']=='HDS'.$i){ //1-8 合数单双号码
			   $this->bds_h($num,$info);
			   }
			    if($info['w_type']=='ZFB'.$i){ //1-8 中发白
			   $this->b_zfb($num,$info);
			   }
			    if($info['w_type']=='FW'.$i){ //1-8 东南西北
			   $this->b_dnxb($num,$info);
			   }
			   
			 
			 }
		 
 		 }
		  public function zh_wdx($sum,$info){  //总和大小
		  
	  $w=substr($sum,-1);
	  
          if($w>4){
		     $dx='D';	 
			}else{
			 $dx='X';	 
			}
			
	      if($dx==$info['t_type']){  //中奖
		  
			  $this->zhongjiang($info);
 			  }else{
			  //不中奖  判断是否反水
 			     $this->fanshui($info);
				 
			 }
       } 
		 
	  public function zh_dx($sum,$info){  //总和大小
	  
          if($sum>=84){
		     $dx='D';	 
			}else{
			 $dx='X';	 
			}
			
	      if($dx==$info['t_type']){  //中奖
			  $this->zhongjiang($info);
 			  }else{
			  //不中奖  判断是否反水
 			     $this->fanshui($info);
				 
			 }
       }
    
	public function zh_ds($sum,$info){  //总和单双
      if($sum%2==0){
		$ds='S';
			}else{
			
      $ds='D';		
		}
	 if($ds==$info['t_type']){  //中奖
			  $this->zhongjiang($info);
 			  }else{
			  //不中奖  判断是否反水
 					  $this->fanshui($info);
 			 }
    }
	
	
	 public function zm($num,$info){  //正码
	
 	if(in_array($info['t_type'],$num)){  //中奖
	
	  $this->zhongjiang($info);
	  }else{
	  //不中奖  判断是否反水
	  $this->fanshui($info);
	 }	 
  }
	 public function lm($num,$info){  //连码
	$lian=substr($info['w_type'], -1); 
	$lian1=substr($info['w_type'], -2,1);
	
	$t_type=explode(',',$info['t_type']);
	$win=0;
	$lost=0;
		$t =$this->getCombinationToString($t_type, $lian);
	
		$money_win=0;
		$count=count($t);
		$money=0;
		$info['money']=round((float)$info['money']/$count,2);
		foreach($t as $k=>$v){
			$t_num=explode(',',$v);
			
			$zhon=array_intersect($t_num,$num);
		
			if(count($zhon)>=$lian){  //中奖
			
			  if($lian1==3){
				  //2连或者三连情况下不中奖
				  $num3[]=$num[0];
				  $num3[]=$num[1];
				  $num3[]=$num[2];
				  $zhon1=array_intersect($t_num,$num3);
				  if(count($zhon1)==$lian1){
					$money_win+=round((float)$info['money']*$info['odds'],2);   
				  }else{
					  //不中奖 
			  $lost++;
			 $money+=$info['money'];  
				  }
				  
			  }elseif($lian1==2){
				  foreach($num as $k=>$v){
					  if(in_array($v,$t_num)){
						  $b_num[]=$k;
					  }
				  }
				  if(abs($b_num[0]-$b_num[1])==1){
					  	$money_win+=round((float)$info['money']*$info['odds'],2); 
				  }else{
					   //不中奖 
					  $lost++;
					 $money+=$info['money'];
				  }
			  }else{
				 $money_win+=round((float)$info['money']*$info['odds'],2); 
			  }
			  
			
			  }else{
			  //不中奖 
			  $lost++;
			 $money+=$info['money'];
			 }
			
		}
		
		$money=$money_win-$money;
	if($money>0){
		$this->zhongjiang_lm($info,$money,$money_win);
	}else{
		//反水比列
		$column=$lost/$count;
		$this->fanshui_lm($info,$column,$money,$money_win);
	}
	
 		 
  }
  

 public function lht($num,$info){  //龙虎和
 $num=substr($info['w_type'],2,1);
 $num1=$num;
 $num2=8-$num;
      if($num[$num1]>$num[$num2]){
			$lht='L';	 
			 }elseif($num[$num1]<$num[$num2]){
				$lht='H';	  
				 }	else{
				$lht='T';	  	 
					 }	
		if($lht==$info['t_type']){  //中奖
			  $this->zhongjiang($info);
 			  }elseif($lht=='T'){
				  //和
				  $this->he($info);
			  }else{
			  //不中奖  判断是否反水
 					  $this->fanshui($info);
 			 }			 
  }
 
 public function bdx($num,$info){  //1-8球大小
	$ax=substr($info['w_type'], -1); 
	$bx=$ax-1;
 		 if($num[$bx]>=11){
		   $bdx='D';	 
			 }else{
			$bdx='X';	 
				 }
 	if($bdx==$info['t_type']){  //中奖
	  $this->zhongjiang($info);
	  }else{
	  //不中奖  判断是否反水
	  $this->fanshui($info);
	 }	 
  }
 public function bds($num,$info){  //1-8球单双
	$ax=substr($info['w_type'], -1); 
	$bx=$ax-1;
		 if($num[$bx]%2==0){
		   $bds='S';	 
			 }else{
			$bds='D';	 
				 }
 	if($bds==$info['t_type']){  //中奖
	  $this->zhongjiang($info);
	  }else{
	  //不中奖  判断是否反水
	  $this->fanshui($info);
	 }	 
  }	 
   public function bds_h($num,$info){  //1-8球合数单双
	
	$ax=substr($info['w_type'], -1); 
	$bx=$ax-1;
	$num=$num[$bx];
	$sum=$num%10+$num/10%10;
		 if($sum%2==0){
		   $bds='S';	 
			 }else{
			$bds='D';	 
				 }
 	if($bds==$info['t_type']){  //中奖
	  $this->zhongjiang($info);
	  }else{
	  //不中奖  判断是否反水
	  $this->fanshui($info);
	 }
		 
	}
    public function bdx_w($num,$info){  //1-8球尾大小
	
	$ax=substr($info['w_type'], -1); 
	$bx=$ax-1;
		 if($num[$bx]>4){
		   $wdx='D';	 
			 }else{
			$wdx='X';	 
				 }
				 if($wdx==$info['t_type']){  //中奖
	  $this->zhongjiang($info);
	  }else{
	  //不中奖  判断是否反水
	  $this->fanshui($info);
	 }
		 
	}
	 public function b_dnxb($num,$info){  //1-8球东南西北
	
	$ax=substr($info['w_type'], -1); 
	$bx=$ax-1;
		 if(in_array($num[$bx],array(01,05,09,13,17))){
		   $wdx=0;	 
			 }elseif(in_array($num[$bx],array(02,06,10,14,18))){
				$wdx=1;	 
			 }elseif(in_array($num[$bx],array(03,07,11,15,19))){
				$wdx=2;	 
			 }else{
			$wdx=3;		 
				 }
				 if($wdx==$info['t_type']){  //中奖
	  $this->zhongjiang($info);
	  }else{
	  //不中奖  判断是否反水
	  $this->fanshui($info);
	 }
		 
	}
	 public function b_zfb($num,$info){  //1-8球中发白
	
	$ax=substr($info['w_type'], -1); 
	$bx=$ax-1;
		 if(in_array($num[$bx],array(01,02,03,04,05,06,07))){
		   $wdx=0;	 
			 }elseif(in_array($num[$bx],array(08,09,10,11,12,13,14))){
				$wdx=1;	 
			 }elseif(in_array($num[$bx],array(15,16,17,18,19,20))){
				$wdx=2;	 
			 }
				 if($wdx==$info['t_type']){  //中奖
	  $this->zhongjiang($info);
	  }else{
	  //不中奖  判断是否反水
	  $this->fanshui($info);
	 }
		 
	}
  
  public function ball($num,$info){  //1-8球单双
	$ax=substr($info['w_type'], -1); 
	$bx=$ax-1;
	 
 	if($num[$bx]==$info['t_type']){  //中奖
	  $this->zhongjiang($info);
	  }else{
	  //不中奖  判断是否反水
	  $this->fanshui($info);
	 }	 
  }	
   public function zhongjiang_lm($info,$money,$money_win){
 	
	//print_r($money);exit;
	$updata_data['is_winning'] = 1;
	$updata_data['status'] = 1;
	$updata_data['js_time'] = time();
	$updata_data['win_money'] = $money;
	M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
	M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$money_win); // 用户的余额增加
	//添加余额变动记录
	$insert_log['m_id']=$info['m_id'];
	$insert_log['change_money']=$money_win;
	$insert_log['change_type']=5;
	$insert_log['add_time']=time();
	$insert_log['remark']='重庆农场中奖 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
	M('account_log')->add($insert_log);
	 }
	  public function fanshui_lm($info,$column,$money,$money_win){
 	  $updata_data['is_winning'] = 2;
	  $updata_data['status'] = 1;
	  //$updata_data['win_money'] = $money;
	  $updata_data['js_time'] = time();
	  M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
	  if($money_win>0){
		  
			M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$money_win); // 用户的余额增加
			//添加余额变动记录
			$insert_log['m_id']=$info['m_id'];
			$insert_log['change_money']=$money_win;
			$insert_log['change_type']=2;
			$insert_log['add_time']=time();
			$insert_log['remark']='重庆农场返回余额 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
			M('account_log')->add($insert_log);
	  }
		if($info['rebate']>0){
			$rebate=round((float)$info['rebate']*$column,2);
			
			
			M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$info['rebate']); // 用户的余额增加
			//添加余额变动记录
			$insert_log['m_id']=$info['m_id'];
			$insert_log['change_money']=$info['rebate'];
			$insert_log['change_type']=2;
			$insert_log['add_time']=time();
			$insert_log['remark']='重庆农场反水 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
			M('account_log')->add($insert_log);
		}
	 }
 public function zhongjiang($info){
 	$money=round((float)$info['money']*$info['odds'],2);
	//print_r($money);exit;
	$updata_data['is_winning'] = 1;
	$updata_data['status'] = 1;
	$updata_data['js_time'] = time();
	$updata_data['win_money'] =round((float)$info['money']*($info['odds']-1),2);
	M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
	M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$money); // 用户的余额增加
	//添加余额变动记录
	$insert_log['m_id']=$info['m_id'];
	$insert_log['change_money']=$money;
	$insert_log['change_type']=5;
	$insert_log['add_time']=time();
	$insert_log['remark']='重庆农场中奖 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
	M('account_log')->add($insert_log);
	 }
	 
	 public function he($info){
 	$money=$info['money'];
	//print_r($money);exit;
	$updata_data['is_winning'] = 3;
	$updata_data['status'] = 1;
	$updata_data['js_time'] = time();
	M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
	M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$money); // 用户的余额增加
	//添加余额变动记录
	$insert_log['m_id']=$info['m_id'];
	$insert_log['change_money']=$money;
	$insert_log['change_type']=5;
	$insert_log['add_time']=time();
	$insert_log['remark']='重庆农场和 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
	M('account_log')->add($insert_log);
	 } 
 public function fanshui($info){
 	  $updata_data['is_winning'] = 2;
	  $updata_data['status'] = 1;
	  $updata_data['js_time'] = time();
	  M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
		if($info['rebate']>0){
			M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$info['rebate']); // 用户的余额增加
			//添加余额变动记录
			$insert_log['m_id']=$info['m_id'];
			$insert_log['change_money']=$info['rebate'];
			$insert_log['change_type']=2;
			$insert_log['add_time']=time();
			$insert_log['remark']='重庆农场反水 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
			M('account_log')->add($insert_log);
		}
	 }


  public function getCombinationToString($arr,$m)
		{
			$result = array();
			if ($m ==1)
			{
			   return $arr;
			}
			
			if ($m == count($arr))
			{
				$result[] = implode(',' , $arr);
				return $result;
			}
				
			$temp_firstelement = $arr[0];
			unset($arr[0]);
			$arr = array_values($arr);
			$temp_list1 = $this->getCombinationToString($arr, ($m-1));
			
			foreach ($temp_list1 as $s)
			{
				$s = $temp_firstelement.','.$s;
				$result[] = $s;
			}
			unset($temp_list1);

			$temp_list2 = $this->getCombinationToString($arr, $m);
			foreach ($temp_list2 as $s)
			{
				$result[] = $s;
			}    
			unset($temp_list2);
			
			return $result;
		}	 
	 
}